package org.opengts.util;

import java.math.BigInteger;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Util {
	static final double POW2_32F = 4294967296.0;
	static final int LOCATION_LENGTH = 8;
	
	public static String encodeLocationToSend(double longitude, double latitude) {
		String lati = encodeLatitude(latitude);
		String longi = encodeLongitude(longitude);
		return convertToHexa(Long.parseLong(lati), LOCATION_LENGTH)
				+ convertToHexa(Long.parseLong(longi), LOCATION_LENGTH);
	}
	
	
	public static String convertStringToHex(String arg) {
	    return String.format("%x", new BigInteger(arg.getBytes()));
	}

	public static String decodeToSaveDataBase(String encLon, String encLat) {
		long longLong = convertToDecimal(encLon);
		long longLat = convertToDecimal(encLat);
		String longi = decodeLongitude(longLong);
		String lati = decodeLatitude(longLat);
		return longi + "," + lati;
	}

	public static long convertToDecimal(String str) {
		long decimal = Long.parseLong(str, 16);
		return decimal;
	}

	public static String fillHexaString(String str, int byteNum) {
		int fillByte = byteNum - str.length();
		char[] filledArray = new char[byteNum];
		int count = 0;
		for (int i = 0; i < fillByte; i++) {
			filledArray[count++] = "0".charAt(0);
		}

		for (int i = 0; i < str.length(); i++) {
			filledArray[count++] = str.charAt(i);
		}
		return new String(filledArray);
	}

	public static String convertToHexa(long num, int byteNum) {
		return fillHexaString(Long.toHexString(num), byteNum);
	}

	public static String decodeLongitude(long encLong) {
		double longitude = ((double) encLong * (360.0 / POW2_32F)) - 180.0;
		return String.valueOf(longitude);
	}

	public static String decodeLatitude(long encLat) {
		double latitude = ((double) encLat * (-180.0 / POW2_32F)) + 90.0;
		return String.valueOf(latitude);
	}

	public static String encodeLongitude(double longitude) {
		long encLong = (long) ((longitude + 180.0) * (POW2_32F / 360.0));
		return String.valueOf(encLong);
	}

	public static String encodeLatitude(double latitude) {
		long encLat = (long) ((latitude - 90.0) * (POW2_32F / -180.0));
		return String.valueOf(encLat);
	}
	
	private static String formatTime(java.util.Date date) {
		SimpleDateFormat sdf = new java.text.SimpleDateFormat(
				"yyyy-MM-dd HH:mm:ss");
		return sdf.format(date);
	}

	public static java.util.Date stringToDate(String str) {
		SimpleDateFormat sdf = new java.text.SimpleDateFormat(
				"yyyy-MM-dd HH:mm:ss");
		try {
			return sdf.parse(str);
		} catch (ParseException e) {
			e.printStackTrace();
			return null;
		}
	}

	public static Date convertTimeStampToDate(long time) {
		String dateAsText = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
				.format(new Date(Integer.parseInt(String.valueOf(String
						.valueOf(time))) * 1000L));
		return stringToDate(dateAsText);
	}


	public static double convertStringHexToDecimal(String hex) {
		return Double.parseDouble(hex);
	}

}
